package com.dmwork.service.interaction.service.impl;

import com.dmwork.common.base.result.R;
import com.dmwork.common.base.result.ResultCodeEnum;
import com.dmwork.service.base.exception.DmworkException;
import com.dmwork.service.interaction.entity.FeedComment;
import com.dmwork.service.interaction.entity.FeedCommentLike;
import com.dmwork.service.interaction.entity.WorkComment;
import com.dmwork.service.interaction.entity.WorkCommentLike;
import com.dmwork.service.interaction.entity.vo.CommentLikeMessageVo;
import com.dmwork.service.interaction.feign.CustomerFeignService;
import com.dmwork.service.interaction.mapper.FeedCommentLikeMapper;
import com.dmwork.service.interaction.service.FeedCommentLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dmwork.service.interaction.service.FeedCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 动态评论的点赞记录表 服务实现类
 * </p>
 *
 * @author 李天
 * @since 2021-05-07
 */
@Service
public class FeedCommentLikeServiceImpl extends ServiceImpl<FeedCommentLikeMapper, FeedCommentLike> implements FeedCommentLikeService {

    @Autowired
    private CustomerFeignService customerFeignService;
    @Autowired
    private FeedCommentService feedCommentService;

    @Override
    public List<CommentLikeMessageVo> getCommentLikeMessageList(List<String> feedCommentLikeIdList) {
        //获取动态部分点赞记录
        List<FeedCommentLike> feedCommentLikeList = baseMapper.selectBatchIds(feedCommentLikeIdList);

        //获取所有点赞的用户的用户名和头像以及被点赞的评论记录
        List<String> customerIdList=new ArrayList<>(); //点赞的用户的id
        List<String> commentIdList=new ArrayList<>(); //被点赞的评论的id
        for (FeedCommentLike feedCommentLike : feedCommentLikeList) {
            customerIdList.add(feedCommentLike.getCustomerId());
            commentIdList.add(feedCommentLike.getCommentId());
        }
        R customerBriefInfoListResult = customerFeignService.getBriefInfoList(customerIdList);
        if (!customerBriefInfoListResult.getSuccess()) {
            throw new DmworkException(ResultCodeEnum.SELECT_CUSTOMER_INFO_ERROR);
        }
        HashMap<String, List<String>> customerBriefInfoMap = (HashMap<String, List<String>>) customerBriefInfoListResult.getData().get("result");
        List<FeedComment> feedCommentList = feedCommentService.listByIds(commentIdList);

        //数据组装
        List<CommentLikeMessageVo> commentLikeMessageVoList=new ArrayList<>();
        for (FeedCommentLike feedCommentLike : feedCommentLikeList) {
            CommentLikeMessageVo commentLikeMessageVo = new CommentLikeMessageVo();
            commentLikeMessageVo.setLikeId(feedCommentLike.getId());
            commentLikeMessageVo.setCustomerId(feedCommentLike.getCustomerId());
            commentLikeMessageVo.setCustomerName(customerBriefInfoMap.get(feedCommentLike.getCustomerId()).get(0));
            commentLikeMessageVo.setCustomerAvatar(customerBriefInfoMap.get(feedCommentLike.getCustomerId()).get(1));
            commentLikeMessageVo.setCommentId(feedCommentLike.getCommentId());
            commentLikeMessageVo.setType(1);
            commentLikeMessageVo.setGmtCreate(feedCommentLike.getGmtCreate());
            for (FeedComment feedComment : feedCommentList) {
                if (commentLikeMessageVo.getCommentId().equals(feedComment.getId())) {
                    commentLikeMessageVo.setCommentContent(feedComment.getContent());
                    commentLikeMessageVo.setFeedId(feedComment.getFeedId());
                    break;
                }
            }
            commentLikeMessageVoList.add(commentLikeMessageVo);
        }
        return commentLikeMessageVoList;
    }
}
